package com.example.springboot.event;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

/**
 * 企业申领印章事件发布器
 * @author xionglei
 * @version 1.0.0
 * 2021年09月06日 09:20
 */
@Component
public class CompanyApplySealEventPublisher implements ApplicationEventPublisherAware, ApplicationContextAware {

    /**
     * 日志管理组件
     */
    private static final Logger log = LoggerFactory.getLogger(CompanyApplySealEventPublisher.class);

    /**
     * 上下文容器
     */
    private ApplicationContext applicationContext;

    /**
     * 事件发布组件
     */
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void setApplicationEventPublisher( ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    /**
     * 广播📢{@link CompanyApplySealEventPublisher}事件
     *
     * @param consumer 消费者
     */
    public void publishEvent(CompanyApplySealConsumer consumer) {
        log.info("企业申领印章事件发布器消费者事件发布开始，参数:{}", JSON.toJSONString(consumer));
        CompanyApplySealEvent event = new CompanyApplySealEvent(applicationContext);
        event.setConsumer(consumer);
        applicationEventPublisher.publishEvent(event);
        log.info("企业申领印章事件发布器事件发布结束");
    }

}
